//
//  SENameCardViewController.swift
//  SmartEyeForDoctor
//
//  Created by qdsg on 2021/5/5.
//

import UIKit

class SENameCardViewController: SEBaseViewController {


    @IBOutlet weak var bgView: UIView! {
        didSet {
            bgView.layer.cornerRadius = 16
            bgView.layer.masksToBounds = true
        }
    }
    @IBOutlet weak var avatorImgView: UIImageView!
    @IBOutlet weak var nameCardImgView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var deptAndJobLabel: UILabel!
    @IBOutlet weak var hospitalLabel: UILabel!
    @IBOutlet weak var shareBtn: SEConfirmBtnView! {
        didSet {
            shareBtn.title = "分享名片"
            shareBtn.delegate = self
        }
    }

    var viewModel = SENameCardViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "我的名片"
        view.backgroundColor = UIColor(hex: "#E2EFEA")
        requestDoctorQrCode()
        // Do any additional setup after loading the view.
    }

    override func setupUI() {
        super.setupUI()
        viewModel.updateData = { status in
            switch status {
            case .success:
                self.updateData()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestDoctorQrCode)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    private func updateData() {
        avatorImgView.kf.setAvatarImage(urlString: viewModel.userInfoModel.imgPath,
                                        placeholder: viewModel.avatarPlaceholder)
        nameLabel.text = viewModel.userInfoModel.name
        deptAndJobLabel.text = "\(viewModel.userInfoModel.deptName)/\(viewModel.userInfoModel.jobTitle)"
        hospitalLabel.text = viewModel.userInfoModel.hospitalName
        nameCardImgView.image = UIImage.base64StringToUIImage(base64String: viewModel.model.qrCodeUrl)
    }

}

// MARK: - SEConfirmBtnViewDelegate

extension SENameCardViewController: SEConfirmBtnViewDelegate {
    func confirmBtnViewAction(view: SEConfirmBtnView) {
        let image = nameCardImgView.image
        guard let img = image else {
            return
        }
        let shareAry: [Any] = [img]
        let share = UIActivityViewController(activityItems: shareAry, applicationActivities: nil)
        present(share, animated: true, completion: nil)
    }
}

// MARK: - APIRequest

extension SENameCardViewController {
    @objc func requestDoctorQrCode() {
        viewModel.requestDoctorQrCode()
    }
}
